package tu.java.hall;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import tu.java.db.DBConnManager;

public class HallSeat {
	
	
	
	private int		hallSeatId;
	private int		seatNum;
	private boolean	status;
	
	
	
	public static boolean addHallSeat(int num, int hallId) throws SQLException {
		boolean status = true;
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement("INSERT INTO TH02_SEATS(CH02_SEAT_NUMBER,CH01_HALL_ID) VALUES (?,?)");
			pstmt.setInt(1, num);
			pstmt.setInt(2, hallId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		
		return status;
	}
	
	
	
	public static Vector<HallSeat> loadAllSeats(int hallId) {
		Vector<HallSeat> seats = new Vector<HallSeat>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement("SELECT CH02_SEAT_ID,CH02_SEAT_NUMBER FROM TH02_SEATS WHERE CH01_HALL_ID=?");
			pstmt.setInt(1, hallId);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				HallSeat hs = new HallSeat();
				hs.hallSeatId = rs.getInt(1);
				hs.seatNum = rs.getInt(2);
				seats.add(hs);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return seats;
	}
	
	
	
	public boolean deleteHallSeat() {
		boolean status = true;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement(" DELETE FROM TH02_SEATS WHERE CH02_SEAT_ID=?");
			pstmt.setInt(1, hallSeatId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	public static boolean deleteAllHallSeats(int hallId) {
		boolean status = true;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement(" DELETE FROM TH02_SEATS WHERE CH01_HALL_ID=?");
			pstmt.setInt(1, hallId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public int getHallSeatId() {
		return hallSeatId;
	}
	
	
	
	public void setHallSeatId(int hallSeatId) {
		this.hallSeatId = hallSeatId;
	}
	
	
	
	public int getSeatNum() {
		return seatNum;
	}
	
	
	
	public void setSeatNum(int seatNum) {
		this.seatNum = seatNum;
	}
	
	
	
	public boolean isStatus() {
		return status;
	}
	
	
	
	public void setStatus(boolean status) {
		this.status = status;
	}
}
